import { AxiosRequestConfig } from 'axios'
import { requestType } from './model'
import service from '../../utils/request'

/**
 * @description FormData请求
 */
export const postFormData = <T = any>({
  url,
  params
}: requestType): Promise<T> => {
  let formData = ''
  const newParams: Record<string, string> = {
    ...params
  }
  if (params) {
    Object.keys(newParams).forEach((key) => {
      formData += `${key}=${newParams[key]}&`
    })
    formData = formData.substring(0, formData.length - 1)
  }
  return service({
    url,
    method: 'POST',
    data: formData
  } as AxiosRequestConfig)
}
